package com.example.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.intellij.psi.JavaRecursiveElementVisitor;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.search.searches.ClassInheritorsSearch;

/**
 * @author duanxiuqing
 * @date 2025/4/13
 */
public class SqlIdFinder {
    public static List<String> findStringsInConcreteImplementation(PsiMethod interfaceMethod) {
        List<String> stringValues = new ArrayList<>();

        // 1. 获取接口方法所在的接口
        PsiClass interfaceClass = interfaceMethod.getContainingClass();
        if (interfaceClass == null) {
            return stringValues;
        }

        // 2. 查找所有直接实现类
        List<PsiClass> implementors = findDirectImplementations(interfaceClass);

        // 3. 在每个实现类中查找对应方法
        for (PsiClass implClass : implementors) {
            final PsiMethod[] methodsByName = implClass.findMethodsByName(interfaceMethod.getName(), false);
            if (methodsByName.length != 0) {
                stringValues.addAll(collectAllStringsInMethod(methodsByName[0]));
            }
        }

        return stringValues;
    }

    private static List<PsiClass> findDirectImplementations(PsiClass interfaceClass) {
        List<PsiClass> implementors = new ArrayList<>();
        if (!interfaceClass.isInterface()) {
            return Collections.singletonList(interfaceClass);
        }
        // 查找所有实现该接口的类
        ClassInheritorsSearch.search(interfaceClass).forEach(implClass -> {
            // 检查是否是直接实现（没有中间接口）
            for (PsiClassType implementedType : implClass.getImplementsListTypes()) {
                if (interfaceClass.equals(implementedType.resolve())) {
                    implementors.add(implClass);
                    break;
                }
            }
            return true;
        });

        return implementors;
    }

    private static List<String> collectAllStringsInMethod(PsiMethod method) {
        List<String> strings = new ArrayList<>();

        method.accept(new JavaRecursiveElementVisitor() {
            @Override
            public void visitLiteralExpression(PsiLiteralExpression expression) {
                if (expression.getType() != null &&
                    "java.lang.String".equals(expression.getType().getCanonicalText())) {
                    String value = (String) expression.getValue();
                    if (value != null) {
                        strings.add(value);
                    }
                }
                super.visitLiteralExpression(expression);
            }

            @Override
            public void visitReferenceExpression(PsiReferenceExpression expression) {
                // 处理静态字符串常量
                PsiElement resolved = expression.resolve();
                if (resolved instanceof PsiField) {
                    PsiField field = (PsiField) resolved;
                    if (field.hasModifierProperty(PsiModifier.STATIC) &&
                        field.getType().equalsToText("java.lang.String")) {
                        PsiExpression initializer = field.getInitializer();
                        if (initializer instanceof PsiLiteralExpression) {
                            String value = (String) ((PsiLiteralExpression) initializer).getValue();
                            if (value != null) {
                                strings.add(value);
                            }
                        }
                    }
                }
                super.visitReferenceExpression(expression);
            }
        });

        return strings;
    }
}
